package org.hxyjs.dao.impl;

import org.hxyjs.dao.ChatRoomDao;
import org.hxyjs.entity.ChatRoom;
import org.hxyjs.mapper.ChatRoomMapper;
import org.hxyjs.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("chatroomDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ChatRoomDaoImpl implements ChatRoomDao{
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ChatRoomMapper chatroomMapper;

    public List<ChatRoom>  getAllChatRooms() throws Exception{ return chatroomMapper.selectByExample(null); }

    @Override
    public ChatRoom getChatRoomByUno(String uno) throws Exception {
        return chatroomMapper.selectByPrimaryKey(uno); }

    @Override
    public List<ChatRoom> getChatRoomsByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) chatroomMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return chatroomMapper.getChatRoomByPage(map);
    }

    @Override
    public Integer deleteChatRoom(String uno) { return chatroomMapper.deleteByPrimaryKey(uno); }

    @Override
    public Integer updateChatRoom(ChatRoom chatroom) { return chatroomMapper.updateByPrimaryKey(chatroom); }



    @Override
    public ChatRoom getChatRoomsWithOrders(String uno) throws Exception {
        return null;
    }

    @Override
    public ChatRoom getAccountByAccountName(String uno) throws Exception {
        return null;
    }

    @Override
    public Integer addChatRoomByUno(ChatRoom chatroom){
        chatroom.setRoomNo(initId());
        System.out.println(chatroom);
        return chatroomMapper.insertSelective(chatroom);
    }
//
//    @Override
//    public ChatRoom getChatRoomsWithOrders(String uno) throws Exception {
//        return chatroomMapper.getChatRoomsWithOrders(uno);
//    }
//    @Override
//    public ChatRoom getAccountByAccountName(String uname) throws Exception {
//        return chatroomMapper.getAccountByAccountName(uname);
//    }
//
//
    public String initId() {
        String maxCano = chatroomMapper.getMaxRoomNo();
        maxCano = Optional.ofNullable(maxCano).orElse("0000"); //如果maxUno 为空 就为0000
        return org.hxyjs.utils.GenNumTools.initId("e", 4, maxCano);
    }
}
